//JS
layui.use(['element', 'layer', 'util'], function () {
    var element = layui.element
        , layer = layui.layer
        , util = layui.util
        , $ = layui.$;

    //头部事件
    util.event('lay-header-event', {
        //左侧菜单事件
        menuLeft: function (othis) {
            layer.msg('展开左侧菜单的操作', {icon: 0});
        }
        , menuRight: function () {
            layer.open({
                type: 1
                , content: '<div style="padding: 15px;">处理右侧面板的操作</div>'
                , area: ['260px', '100%']
                , offset: 'rt' //右上角
                , anim: 5
                , shadeClose: true
            });
        }
    });

    element.on('nav(menu)', function (elem) {
        //console.log(elem.getAttribute("meun_id")); //得到当前点击的DOM对象
        var id = $(this).attr("meun_id");
        if (typeof (id) != "undefined") {
            element.tabAdd('demo1', {
                title: '新选项' + (Math.random() * 1000 | 0) //用于演示
                , content: '<iframe src="test.html"  scrolling="auto" frameborder="0"></iframe>'
                , id: id//实际使用一般是规定好的id，这里以时间戳模拟下z
            })
            element.tabChange('demo1', id);
        }
    });


    //组装子菜单的方法
    function loadchild(obj) {
        if (obj == null) {
            return;
        }
        var content = '';
        if (obj.childrenList != null && obj.childrenList.length > 0) {
            content += '<dl class="layui-nav-child">';
        } else {
            content += '<dl>';
        }

        if (obj.childrenList != null && obj.childrenList.length > 0) {
            $.each(obj.childrenList, function (i, note) {
                content += '<dd>';
                content += '<a href="' + note.menuUrl + '" target="main" >' + note.menuTitle + '</a>';
                if (note.childrenList == null) {
                    return;
                }
                content += loadchild(note);
                content += '</dd>';
            });

            content += '</dl>';
        }
        console.log(content);
        return content;
    }

    //获取所有的菜单
    $.ajax({
        type: "GET",
        url: "/system/menu/getAllMenus",
        dataType: "json",
        success: function (resp) {
            var data = resp.data;
            //先添加所有的主材单
            $.each(data, function (i, obj) {
                var content = '<li class="layui-nav-item">';
                if (obj.type == 2) {
                    content += '<a href="' + obj.menuUrl + '" target="main" ><i class="layui-icon layui-icon-face-smile"></i>' + obj.menuTitle + '</a>';
                } else if (obj.type == 1) {
                    content += '<a href="javascript:;" >' + obj.menuTitle + '</a>';
                }
                //这里是添加所有的子菜单
                content += loadchild(obj);
                content += '</li>';
                $(".layui-nav-tree").append(content);
            });
            element.init();
        },
        error: function (jqXHR) {
            aler("发生错误：" + jqXHR.status);
        }
    });

    window.logout = function () {
        $.ajax({
            type: "GET",
            url: "/logout",
            dataType: "json",
            success: function (resp) {
                console.log(resp);
                if (resp.code == '00000') {
                    window.location.href = '/login.html'
                }
            },
            error: function (jqXHR) {
                layer.msg("发生错误：" + jqXHR.status);
            }
        });
    }

});